package com.moma.chatting.core;

import java.util.Calendar;
import java.util.Date;

public class User extends Object{
	
	private int userExpiredTimeType;
	private int userExpiredTimeNums;
	public User(){
		this(Calendar.MINUTE, -5);
	}
	
	public User(int userExpiredTimeType, int userExpiredTimeNums){
		this.lastReportedTime = new Date();
		this.userExpiredTimeType = userExpiredTimeType;
		this.userExpiredTimeNums = - (Math.abs(userExpiredTimeNums));
	}
	
	private String friendlyName;
	public void setFriendlyName(String value){
		this.friendlyName = value;
	}
	public String getFriendlyName(){
		return this.friendlyName;
	}
	
	private String uniqueName;
	public void setUniqueName(String value){
		this.uniqueName = value;
	}
	public String getUniqueName(){
		return this.uniqueName;
	}
	
	private Date lastReportedTime;
	public Date getLastReportedTime(){
		return this.lastReportedTime;
	}
	
	public void reportOnline(){
		this.lastReportedTime = new Date();
	}
	
	// this method should keep naturally thread-safe.
	public boolean getIsOnline(){
		User user = this;
		Calendar nowTime = Calendar.getInstance();
		//nowTime.add(Calendar.MINUTE, -5); // TODO: Jimmy Mo - Hard coded
		nowTime.add(this.userExpiredTimeType, this.userExpiredTimeNums);
		Date expiredDate = (Date)nowTime.getTime();
		Date reportedDate = user.getLastReportedTime();
		if (reportedDate.before(expiredDate)){ //expired
			return false;
		}
		return true;
	}
	
	@Override
	public int hashCode(){
		return this.uniqueName.hashCode();
	}
	
	@Override
	public boolean equals(Object obj){
		if (obj == null)
			return false;
		if (!(obj instanceof User))
			return false;
		
		return this.uniqueName.equals(((User)obj).uniqueName);
	}
}

